package com.pkg;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;


/**
 * 由B加密数据发送给A
 */
public class B2A extends Codec {

    /**
     * A的公钥
     *
     * @return
     */
    @Override
    protected String getPublicKey() {
        return "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJEa4t9QIC7DWsNXm9aI5usAIp2LFGqRgVcVzC" +
                "HZFEZ+AAO4g/SR3bJu41LYxEOH6RVKoH9m3R8vX0jLM9cDdvhGNyv0F8TilFCmAPS+iXeyFSApm7" +
                "R8w9p0gCB/nBmYoyVA5FsoihnzAW0csn+ldGzQgouKhSAIgpqJzqcgUylQIDAQAB";
    }

    /**
     * B的私钥
     *
     * @return
     */
    @Override
    protected String getPrivateKey() {
        return "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJx+mNw23J5JFEfFpPvBm0z56yT2" +
                "UYjC5qGn45MXckF8beVT4YsyJeCsk6jPVTVqzShQZF2g2tgbuqDSP0heFrNkh1h2Vkt5Kmll77GM" +
                "SXvK0D90dvAWAl7XY+SIJo79xW5i8loYWSyMwwWHhlZM42naxbSXTCXcDXXFCvLlWRLfAgMBAAEC" +
                "gYA8JzFMnE3rDb4c5GzJVJ8EVKA9Ie0vO3N5OdZxCLcjUtfPKZHtdkY2+T78yU1AbiAT2erqTlUw" +
                "cmAnRwmKIJhqnBO99z3FOIMwxucoQMsuSgetUlOUQHgSs320HpCIJbc938xdwFi/u23Ii3UEIb9X" +
                "NLORhv1KJkwsd/9atn2h4QJBANTpJklj+Ee+b1XlUDAap3rSDJItW6aL2R5Z+ezkxdBor2bqyshs" +
                "BjPe3Jq5CI+AABuP2JfXPio2s3dqYQmXn88CQQC8Komp2tXGyLH650xa4LNFwoL7zl2RflCuAiqg" +
                "aa+qytbLY9lJdUDjtskCnWcJS94LZl9FAnxSyX1Ewd1V+Y/xAkEAsCohpcPz4NjvIjFVAhhrzzIR" +
                "eLnTBaDdC0IDi0FXc7PKvesz0lWfZDfYbKUBMksEy8lfu3M7Wo5cPEhm9hrg/QJATdP+Wr4D/BOY" +
                "4iF3SIN2Qx1gFgzWUM6BSmfk/7KXETqSu0+SWTrhWw6Id8dpDcROlEKEl0Oe7pMeeCkDhoMR0QJA" +
                "fhF6sjhVgF55yEAg8O8d4Wqa3FfpdXBmkAComCVo0MQjrHrdkNRnKp2b4xoDd2n5ogMbTkFk9mZk" +
                "Q1Qzdde19A==";
    }

    public static Map<String, Object> initKey() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();


        Map<String, Object> map = new HashMap<String, Object>();
        map.put(PUBLIC_KEY, publicKey);
        map.put(PRIVATE_KEY, privateKey);
        return map;

    }

    final static String PUBLIC_KEY = "PUBLIC_KEY";
    final static String PRIVATE_KEY = "PRIVATE_KEY";

    public static void createKey() throws Exception {
        B2A codec = new B2A();

        Map<String, Object> keyMap = initKey();

        Key pukey = (Key) keyMap.get(PUBLIC_KEY);
        Key prkey = (Key) keyMap.get(PRIVATE_KEY);

        System.out.println(codec.encryptBASE64(pukey.getEncoded()));
        System.out.println(codec.encryptBASE64(prkey.getEncoded()));
    }

    public static void main(String[] args) throws Exception {
//        createKey();


        B2A codec = new B2A();

        String a = "65f8dda0e6ebc98f7dc9df05afa0745e18479d96950ccc717e4971dfd87e5ec855f5b82a5065d528f2cd91ef378d6fc6eb029ded64b907a9b81b89cee7b8a1d75ae10cac6dd4dbc017b939c0bc5dcfa37f45b056b1814386084cc026cdc8b525631276b649cb8bad41088d91a71321d65d253ee73f60c124b084ab45fca0ae16";
        String b = "6787f23d80c9eb74";

        String str = codec.decode(a, b);
        System.out.println(str);


    }
}
